package net.learn.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.system.service.QueSubjectService;
import net.learn.system.vo.QueSubjectVO;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.query.QueSubjectQuery;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 科目管理
 *
 * @author 李立志
 */
@RestController
@RequestMapping("que/subject")
@Tag(name = "科目管理")
@AllArgsConstructor
public class QueSubjectController {
    private final QueSubjectService queSubjectService;
    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Result<PageResult<QueSubjectVO>> page(@Valid QueSubjectQuery query) {
        PageResult<QueSubjectVO> page = queSubjectService.page(query);

        return Result.ok(page);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('que:subject:save')")
    public Result<String> save(@RequestBody @Valid QueSubjectVO vo) {
        queSubjectService.save(vo);

        return Result.ok();
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('que:subject:list')")
    public Result<List<QueSubjectVO>> list() {
        List<QueSubjectVO> list = queSubjectService.getList(new QueSubjectQuery());

        return Result.ok(list);
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('que:subject:update')")
    public Result<String> update(@RequestBody @Valid QueSubjectVO vo) {
        queSubjectService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('que:subject:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        queSubjectService.delete(idList);

        return Result.ok();
    }
}